<?php
/*
 * Created on Jul 17, 2007
 *
 * -> Creaza baza de date si structura de start a acesteia <-
 *
 */

include("../classes/constants.php");

$host=DB_SERVER;
$user=DB_USER;
$password=DB_PASS;
$db=DB_NAME;

mysql_connect($host, $user, $password) or die("Connection failed");

mysql_query("CREATE DATABASE `".$db."` ;");
if (mysql_errno())
    {
    echo mysql_error() . "<br>";
    }
else
    {
    echo("Database ".$db." succesfuly created<br>");
    }

mysql_select_db($db) or die("Failed to open db");

//*************Tabele utilizatori********

//--------tabel utilizatori-----
$sql_query=
    "CREATE TABLE users (
 username varchar(30) primary key,
 password varchar(32),
 userid varchar(32),
 userlevel tinyint(1) unsigned not null,
 email varchar(50),
 timestamp int(11) unsigned not null
);";
mysql_query($sql_query);

if (mysql_errno())
    {
    echo mysql_error() . "<br>";
    }
else
    {
    echo("Table users succesfuly created<br>");
    }

//-------tabel membrii on-line------
$sql_query="CREATE TABLE active_users (
 username varchar(30) primary key,
 timestamp int(11) unsigned not null
);";
mysql_query($sql_query);

if (mysql_errno())
    {
    echo mysql_error() . "<br>";
    }
else
    {
    echo("Table active_users succesfuly created<br>");
    }

//-------tabel vizitatrori on-line------
$sql_query="CREATE TABLE active_guests (
 ip varchar(15) primary key,
 timestamp int(11) unsigned not null
);";
mysql_query($sql_query);

if (mysql_errno())
    {
    echo mysql_error() . "<br>";
    }
else
    {
    echo("Table active_guests succesfuly created<br>");
    }

//-------tabel utilizatori cu restrictie acces
$sql_query="CREATE TABLE banned_users (
 username varchar(30) primary key,
 timestamp int(11) unsigned not null
);";
mysql_query($sql_query);

if (mysql_errno())
    {
    echo mysql_error() . "<br>";
    }
else
    {
    echo("Table active_guests succesfuly created<br>");
    }
    
//----------tabel proiecte
$sql_query="CREATE TABLE `proiecte` (
  `idproiect` INTEGER UNSIGNED NOT NULL DEFAULT 0,
  `numeproiect` VARCHAR(45) NOT NULL DEFAULT '',
  `owner` VARCHAR(45) NOT NULL DEFAULT '',
  PRIMARY KEY(`idproiect`)
)ENGINE = MYISAM;";
mysql_query($sql_query);

if (mysql_errno()){
    echo mysql_error() . "<br>";
}else{
    echo("Table proiecte succesfuly created<br>");
}

$sql_query="ALTER TABLE `proiecte` MODIFY COLUMN `owner` VARCHAR(45) CHARACTER SET latin1 COLLATE latin1_swedish_ci,
 ADD CONSTRAINT `owner` FOREIGN KEY `owner` (`owner`)
    REFERENCES `users` (`username`)
    ON DELETE RESTRICT
    ON UPDATE CASCADE;";
mysql_query($sql_query);

if (mysql_errno()){
    echo mysql_error() . "<br>";
}else{
    echo("Table proiecte succesfuly updated<br>");
}

/*
ALTER TABLE `xmldocument`.`proiecte` MODIFY COLUMN `idproiect` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT;


*/
$sql_query="ALTER TABLE `xmldocument`.`proiecte` MODIFY COLUMN `idproiect` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT;";
mysql_query($sql_query);

if (mysql_errno()){
    echo mysql_error() . "<br>";
}else{
    echo("Table proiecte succesfuly updated<br>");
}

?>
